﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Libero.FusionCharts;
using System.Data;
using Libero.FusionCharts.Abstract;
using Libero.FusionCharts.Template;

namespace MvcApplication1.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewData["Message"] = "Welcome to ASP.NET MVC!";

            return View();
        }

        #region [ Single series datasource ]

        public ActionResult Sample_SS_Datasource()
        {
            // Create a chart
            Pie2DChart oChart = new Pie2DChart();

            // Set properties
            oChart.Background.BgColor = "ffffff";
            oChart.Background.BgAlpha = 50;
            oChart.ChartTitles.Caption = "Top seller (%)";
            oChart.ChartTitles.SubCaption = "November";

            // Set a template
            oChart.Template = new Libero.FusionCharts.Template.OfficeTemplate();

            // Set data
            DataTable dtSample = GetSalesDataTable();
            oChart.DataSource = dtSample;
            oChart.DataTextField = "SallesmanName";
            oChart.DataValueField = "PercentTotalSell";

            //Load it into ViewData.
            ViewData["MyChart"] = oChart;

            return View();
        }

        #region [ Auxiliar ]
        private DataTable GetSalesDataTable()
        {
            DataTable dt = new DataTable();

            dt.Columns.Add("SallesmanName", typeof(String));
            dt.Columns.Add("PercentTotalSell", typeof(int));


            dt.Rows.Add("Alan", 9);
            dt.Rows.Add("Tom", 15);
            dt.Rows.Add("Tete", 25);
            dt.Rows.Add("Ana", 51);

            return dt;
        }
        #endregion

        #endregion

        #region [ Single series coding ]

        public ActionResult Sample_SS_Code()
        {
            // Create a chart
            Column3DChart oChart = new Column3DChart();

            // Set properties
            oChart.Background.BgColor = "ffffff";
            oChart.Canvas3D.CanvasBgColor = "eeeeee";
            oChart.ChartTitles.Caption = "Top seller";
            oChart.ChartTitles.SubCaption = "November";

            // Set a template
            oChart.Template = new Libero.FusionCharts.Template.OfficeTemplate();

            // Set data
            foreach (Sales s in GetSales())
            {
                oChart.Set.Add(new SSSetElement { Name = s.SalesmanName, Value = s.TotalSold, ShowName = true });
            }

            // Add trend lines
            oChart.TrendLines.Add(new TrendLineElement { StartValue = 530, EndValue = 533, Color = "33dd33", DisplayValue = "Very good" });
            oChart.TrendLines.Add(new TrendLineElement { StartValue = 200, EndValue = 203, Color = "dd3333", DisplayValue = "Very bad" });

            //Load it into ViewData.
            ViewData["MyChart"] = oChart;

            return View();
        }

        #region [ Auxiliar ]
        private List<Sales> GetSales()
        {
            List<Sales> s = new List<Sales>();

            s.Add(new Sales { SalesmanName = "Alan", TotalSold = 220m });
            s.Add(new Sales { SalesmanName = "Tom", TotalSold = 373m });
            s.Add(new Sales { SalesmanName = "Tete", TotalSold = 150m });
            s.Add(new Sales { SalesmanName = "Ana", TotalSold = 523m });

            return s;
        }
        private class Sales
        {
            public string SalesmanName;
            public decimal TotalSold;
        }
        #endregion

        #endregion

        #region [ Multi series datasource ]

        public ActionResult Sample_MS_Datasource()
        {
            MSBar2DChart oChart = new MSBar2DChart();

            // Set properties
            oChart.Background.BgColor = "ffffff";
            oChart.Background.BgAlpha = 50;
            oChart.ChartTitles.Caption = "# Mobile in last months";

            // Set a template
            oChart.Template = new Libero.FusionCharts.Template.OfficeLightTemplate();

            // Set data   *DataTable OR IList<T>
            DataTable dtSample = GetSalesDataTableMS();
            oChart.DataSource = dtSample;
            oChart.DataCategoryTextField = "Month";
            oChart.DataSeriesTextField = "Brand";
            oChart.DataSeriesValueField = "Quantity";

            //Load it into ViewData.
            ViewData["MyChart"] = oChart;

            return View();
        }

        #region [ Auxiliar ]
        private DataTable GetSalesDataTableMS()
        {
            DataTable dt = new DataTable();

            dt.Columns.Add("Brand", typeof(String));
            dt.Columns.Add("Month", typeof(String));
            dt.Columns.Add("Quantity", typeof(int));

            dt.Rows.Add("Sansung", "08/2010", 2000);
            dt.Rows.Add("Nokia", "08/2010", 3000);
            dt.Rows.Add("Sansung", "09/2010", 1300);
            dt.Rows.Add("Nokia", "09/2010", 2100);
            dt.Rows.Add("Sansung", "10/2010", 3000);
            dt.Rows.Add("Nokia", "10/2010", 3400);
            dt.Rows.Add("Sansung", "11/2010", 5200);
            dt.Rows.Add("Nokia", "11/2010", 7200);

            return dt;
        }
        #endregion

        #endregion

        #region [ Multi series coding ]

        public ActionResult Sample_MS_Code()
        {
            MSColumn3DChart oChart = new MSColumn3DChart();

            // Set properties
            oChart.Background.BgColor = "ffffff";
            oChart.Background.BgAlpha = 50;
            oChart.ChartTitles.Caption = "Categories by country";
            oChart.ChartTitles.SubCaption = "Annual report (US$)";

            // Set a template
            oChart.Template = new Libero.FusionCharts.Template.OceanTemplate();

            // Set data
            // Add Categories Data.
            oChart.CategorySet.FontColor = "0000FF";
            oChart.CategorySet.CategoryElementSet.Add(new MSCategoryElement("Brazil"));
            oChart.CategorySet.CategoryElementSet.Add(new MSCategoryElement("India"));
            oChart.CategorySet.CategoryElementSet.Add(new MSCategoryElement("China"));

            // Add Series data in the correct order.
            MSDataSet msDs;
            msDs = new MSDataSet("Eletronics", "ff0000");
            msDs.SetElementSet.Add(new MSSetElement(15000));
            msDs.SetElementSet.Add(new MSSetElement(30000));
            msDs.SetElementSet.Add(new MSSetElement(45000));
            oChart.DataSetList.Add(msDs);

            msDs = new MSDataSet("Computers", "00ff00");
            msDs.SetElementSet.Add(new MSSetElement(80000));
            msDs.SetElementSet.Add(new MSSetElement(50000));
            msDs.SetElementSet.Add(new MSSetElement(20000));
            oChart.DataSetList.Add(msDs);

            msDs = new MSDataSet("Mobiles", "0000ff");
            msDs.SetElementSet.Add(new MSSetElement(15000));
            msDs.SetElementSet.Add(new MSSetElement(30000));
            msDs.SetElementSet.Add(new MSSetElement(70000));
            oChart.DataSetList.Add(msDs);

            //Load it into ViewData.
            ViewData["MyChart"] = oChart;

            return View();
        }

        #endregion

        #region [ Ajax sample ]

        public ActionResult Sample_Ajax()
        {
            ViewData["MyChart"] = GetSalesChart("2010", "Area2DChart", "OfficeTemplate");

            return View();
        }

        public ActionResult GetSalesXmlData(string period, string chartType, string chartTemplate)
        {
            return this.Content(GetSalesChart(period, chartType, chartTemplate).ToXML(), "text/xml");
        }

        private SingleSeriesChart GetSalesChart(string period, string chartType, string chartTemplate)
        {
            SingleSeriesChart oChart;

            switch (chartType)
            {
                case "Area2DChart": oChart = new Area2DChart(); break;
                case "Bar2DChart": oChart = new Bar2DChart(); break;
                case "Column2DChart": oChart = new Column2DChart(); break;
                case "Column3DChart": oChart = new Column3DChart(); break;
                case "LineChart": oChart = new LineChart(); break;
                default: throw new Exception("chart type not expected.");
            }

            switch (chartTemplate)
            {
                case "OfficeTemplate": oChart.Template = new OfficeTemplate(); break;
                case "OfficeLightTemplate": oChart.Template = new OfficeLightTemplate(); break;
                case "OfficeDarkTemplate": oChart.Template = new OfficeDarkTemplate(); break;
                case "OceanTemplate": oChart.Template = new OceanTemplate(); break;
                default: throw new Exception("chart template not expected.");
            }

            oChart.ChartTitles.Caption = "Anual Sales";
            oChart.ChartTitles.SubCaption = "Period 2010";

            oChart.DataSource = GetData(period).ToList();
            oChart.DataTextField = "Month";
            oChart.DataValueField = "Total";

            return oChart;
        }

        private IEnumerable<Sales2> GetData(string period)
        {
            int p = int.Parse(period); // sort a random number by period 
            yield return new Sales2 { Month = "Jan", Total = p == 2010 ? 21 : 25 };
            yield return new Sales2 { Month = "Feb", Total = p == 2009 ? 24 : 21 };
            yield return new Sales2 { Month = "Mar", Total = p == 2008 ? 25 : 24 };
            yield return new Sales2 { Month = "Apr", Total = p == 2010 ? 24 : 25 };
            yield return new Sales2 { Month = "Mai", Total = p == 2009 ? 21 : 20 };
            yield return new Sales2 { Month = "Jun", Total = p == 2010 ? 20 : 21 };
            yield return new Sales2 { Month = "Jul", Total = p == 2009 ? 19 : 23 };
            yield return new Sales2 { Month = "Aug", Total = p == 2008 ? 22 : 24 };
            yield return new Sales2 { Month = "Sep", Total = p == 2009 ? 25 : 25 };
            yield return new Sales2 { Month = "Oct", Total = p == 2010 ? 25 : 22 };
            yield return new Sales2 { Month = "Nov", Total = p == 2008 ? 24 : 20 };
            yield return new Sales2 { Month = "Dec", Total = p == 2009 ? 20 : 30 };
        }

        private class Sales2
        {
            public string Month { get; set; }
            public decimal Total { get; set; }
        }

        #endregion
    }
}
